package com.chat.dto;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.chat.entity.Users;
import com.chat.service.UsersService;
import com.chat.util.Util;
import com.chat.vo.ServerReturnVo;
import com.chat.vo.TokenVo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Filter;

@Component
public class UsersDto {
    @Autowired
    private UsersService usersService;
    public String login(Users users){
        ServerReturnVo serverReturnVo = new ServerReturnVo(500,"登录失败");
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> UserMap=this.UsersToMap(users);
        List resArr=usersService.selectByMap(UserMap);
        if(resArr.size()!=0){
            Users res= (Users) resArr.get(0);
            map.put("id",res.getId());
            map.put("username",res.getUsername());
            TokenVo tokenVo=Util.getToken(map);;
            if(tokenVo.isSucceed==true){
                serverReturnVo.setCode(0);
                serverReturnVo.setData(tokenVo.data);
            }
        }
        return JSON.toJSONString(serverReturnVo, JSONWriter.Feature.WriteMapNullValue);
    }


    public String isLogin(String token){
        System.out.println(token);
        TokenVo tokenvo=Util.verifyToken(token);
        ServerReturnVo serverReturnVo = new ServerReturnVo(500,"登录无效");
        System.out.println(tokenvo.data);
        if(tokenvo.isSucceed==true){
            serverReturnVo.data="成功";
            serverReturnVo.code=0;
        }
        return JSON.toJSONString(serverReturnVo);
    }
    public String reg(Users users){
        ServerReturnVo serverReturnVo = new ServerReturnVo(500,"注册失败");
        Map<String,Object> map=this.UsersToMap(users,true,false);
        List resArr=usersService.selectByMap(map);
        if(resArr.size()==0){
            try {
                if(usersService.insert(users)==1){
                    List<Users> findId=usersService.selectByMap(map);
                    if(findId.size()!=0){
                        map.put("id",findId.get(0).getId());
                        map.put("username",findId.get(0).getUsername());
                        map.remove("password");
                        System.out.println("tokenMap"+map);
                        TokenVo tokenVo=Util.getToken(map);
                        if(tokenVo.isSucceed==true){
                            serverReturnVo.setCode(0);
                            serverReturnVo.setData(tokenVo.data);
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println("插入失败");
                System.out.println(e);
            }
        }
        return JSON.toJSONString(serverReturnVo, JSONWriter.Feature.WriteMapNullValue);
    }

    private Map UsersToMap(Users users){//用户对象生成map对象
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("username",users.getUsername());
        map.put("password",users.getPassword());
        return map;
    }
    private Map UsersToMap(Users users,Boolean NoFollowUsername,Boolean NoFollowPassword){//用户对象生成map对象
        Map<String,Object> map=new HashMap<String,Object>();
        if(NoFollowUsername==true){
            map.put("username",users.getUsername());
        }
        if(NoFollowPassword==true){
            map.put("password",users.getPassword());
        }
        return map;
    }
    public String getUserInfo(String token){
        System.out.println(token);
        TokenVo tokenvo=Util.verifyToken(token);
        ServerReturnVo serverReturnVo = new ServerReturnVo(500,"获取失败");
        System.out.println(tokenvo.data);
        if(tokenvo.isSucceed==true){
            Map <String,Object> tokenMap=(Map<String,Object>)tokenvo.data;
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",tokenMap.get("id").toString().replace("\"",""));
//            map.put("username",tokenMap.get("username").toString().replace("\"",""));
//            map.put("password",(String)tokenMap.get("password").toString().replace("\"",""));
            System.out.println(map);
            List result=usersService.selectByMap(map);
            System.out.println(result);
            if(result.size()!=0){
                JSONObject obj=JSON.parseObject(JSON.toJSONString(result.get(0)));
                obj.remove("password");
                serverReturnVo.data=obj;
                serverReturnVo.code=0;
            }
        }
        return JSON.toJSONString(serverReturnVo);
    }
}
